package com.stable.actor.remotedemo;

import com.stable.actor.core.StableActor;
import com.stable.actor.network.ServiceResponse;
import lombok.extern.slf4j.Slf4j;
/**
 * 支付服务Actor
 */
@Slf4j
public class PaymentServiceActor extends StableActor {

    public PaymentServiceActor() {
//        super();
    }
    @Override
    protected void setupMessageHandlers() {

    }

    @Override
    public void handleMessage(Object message) {
        if (message instanceof PaymentRequest) {
            PaymentRequest request = (PaymentRequest) message;
            log.info("💰 支付服务处理: " + request.getOrderId() + ", 金额: " + request.getAmount());

            // 模拟支付处理
            try {
                Thread.sleep(800); // 模拟处理时间

                // 模拟90%的成功率
                boolean success = Math.random() > 0.1;
                String responseMsg = success ?
                        "支付成功 - 订单: " + request.getOrderId() + ", 金额: " + request.getAmount() :
                        "支付失败 - 订单: " + request.getOrderId() + ", 余额不足";

                // 发送结构化响应
                ServiceResponse response = new ServiceResponse("payment", success, responseMsg);
                getSender().tell(response, getSelf());

            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                getSender().tell(new ServiceResponse("payment", false, "支付处理被中断"), getSelf());
            }
        }
    }

    @Override
    protected void preStart() {
        log.info("🚀 支付服务启动完成");
    }


}




